﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuaManSys.Model;
using Microsoft.Reporting.WebForms;

namespace QuaManSys.InnerInfo
{
    public partial class InnerInfoQueryDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          //  string query = Request.QueryString["qualityInfoId"].ToString();
          //  List<InnerResponsibleDepart> dataList = respondDepart(query);
          //  Repeater s = (Repeater)FormView1.Controls[FormView1.Controls.Count - 1].FindControl("Repeater2");
          //  Label Label11 = (Label)FormView1.Controls[FormView1.Controls.Count - 1].FindControl("Label11");
          //  //Repeater w = (Repeater)FormView1.Controls[FormView1.Controls.Count - 1].FindControl("Repeater3");
          //  //Label ss = (Label)w.Controls[w.Controls.Count - 1].FindControl("Label11");
          //  s.DataSource = dataList;
          //  s.DataBind();
          //  List<string> component = focusDepart(query);
          //  if (component.Count == 1) Label11.Text = component.First();
          //  else
          //  {
          //      string m = "";
          //  foreach (string c in component)
          //  {
          //      if (m == "") m = c;
          //      else m += '|' + c;


            
          //  }
          //  Label11.Text = m;
          //}
           




          
        }





        public List<InnerQualityInfo> GetInfo(string infoId )
        {
            var db = new QuaManSysEntities();
            IQueryable<InnerQualityInfo> query = db.InnerQualityInfo;

            query = query.Where(p => p.qualityInfoId == infoId);

            return query.ToList(); ;

        }


        public List<InnerResponsibleDepart> respondDepart(string infoid)
        {
            var db = new QuaManSysEntities();
            List<InnerResponsibleDepart> query = db.InnerResponsibleDepart.ToList() ;

            var qery = from p in query
                       where p.qualityInfoId == infoid && p.isResponDepart == 1
                       select p;

          return qery.ToList();

        }


        public List<string> focusDepart(string infoid)
        {
            var db = new QuaManSysEntities();
            List<InnerResponsibleDepart> query = db.InnerResponsibleDepart.ToList();

            var qery = from p in query
                       where p.qualityInfoId == infoid && p.isResponDepart == 0
                       select p.department;

            return qery.ToList();

        }



        /// <summary>
        /// 将数据库中关注部门的信息组合
        /// </summary>
        /// <returns></returns>
        public List<FocusDepart> showFocusdepart()
        {
            string query = Request.QueryString["qualityInfoId"].ToString();
        
            List<FocusDepart> query1 = new List<FocusDepart>();
            List<string> component = focusDepart(query);
            if (component.Count == 1) 
            { 
                FocusDepart p=new FocusDepart();
                p.focusdepatment=component.First();
                query1.Add(p);
                return query1;
            }
            else
            {
                string m = "";
                foreach (string c in component)
                {
                    if (m == "") m = c;
                    else m += '|' + c;



                }
                 FocusDepart q=new FocusDepart();
                q.focusdepatment=m;
                query1.Add(q);
                return query1;
            }

        }


/// <summary>
/// 绑定报表参数
/// </summary>
        public List<string> AutoFillProjectRel(string projectNo)
        {

            List<string> query=new List<string>();

            using (var context = new QuaManSysEntities())
            {
                //try
                //{
                //    var result = from c in context.Tab_ProjectNo
                //                 where projectNo == c.projectNo
                //                 select new { c.machinemodelNo, c.machineNo };
                //    query.Add(result.First().machinemodelNo);
                //   query.Add(result.First().machineNo);
                 
                //}
                //catch (System.Data.EntitySqlException ex)
                //{
                //    Console.WriteLine(ex.ToString());
                 
                //}
            }
            return query;
        }

        protected void ReportViewer1_Load1(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ReportViewer1.LocalReport.ReportEmbeddedResource = "QuaManSys.InnerInfo.Report1.rdlc";
                string query = Request.QueryString["qualityInfoId"].ToString();
                var resultSet1 = GetInfo(query);
                var resultSet2 = respondDepart(query);
                var resultSet3 = showFocusdepart();
                List<string> resultSet4= AutoFillProjectRel(resultSet1.First().projectNo);
                ReportParameter machinemodelNo = new ReportParameter("machinemodelNo", resultSet4[0]);
                ReportParameter machineNo = new ReportParameter("machineNo", resultSet4[1]);
                this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { machinemodelNo, machineNo});


                this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", resultSet1));
                this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet2", resultSet2));
                this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet3", resultSet3));
            
              
                this.ReportViewer1.ZoomMode = Microsoft.Reporting.WebForms.ZoomMode.PageWidth;
               

            }

        }

    }
}